<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix card-header">
        <span>文章分类</span>
        <el-button type="primary" size="medium" @click="addVisible = true">添加分类</el-button>
      </div>
      <!-- 卡片的内容区，放表格 -->
      <el-table :data="cateList" border stripe>
        <!-- 第1列 prop表示数据项的名字； label是表头 -->
        <el-table-column type="index" label="序号" width="100"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <!-- 文章分类列表操作按钮 -->
        <el-table-column label="操作">
          <template v-slot="obj">
            <!-- obj.row 表示当前行的数据信息 -->
            <el-button type="button" size="mini" @click="deleteRow(obj.row.id)"> 删除 </el-button>
            <el-button type="button" size="mini" @click="editCategory(obj.row)"> 修改 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 弹出层 -->
    <el-dialog title="提示" :visible.sync="addVisible" width="30%" :before-close="resetAddForm">
      <span>
        <el-form label-width="80px" :model="addData" :rules="addFormRules" ref="formRef">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="addData.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="addData.cate_alias"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改分类的对话框 -->
    <el-dialog
      title="修改类别"
      :visible.sync="editVisible"
      width="35%"
      :before-close="resetEditForm"
    >
      <!-- 这里放添加的表单 -->
      <el-form :model="editData" :rules="addFormRules" label-width="80px" ref="editRef">
        <!-- 第一项：分类名称 -->
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editData.cate_name"></el-input>
        </el-form-item>
        <!-- 第二项：分类别名 -->
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editData.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetEditForm">取 消</el-button>
        <el-button type="primary" @click="updateCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryAPI,
  addCategoryAPI,
  deleteCategoryAPI,
  updateCategoryAPI
} from '@/api/category'
export default {
  data() {
    return {
      cateList: [],
      addVisible: false,
      editVisible: false,
      // 弹出层数据项
      addData: {
        cate_name: '', // 分类名称
        cate_alias: '' // 分类别名
      },
      // 修改表单的数据对象
      editData: {},

      // 弹出层验证规则
      addFormRules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称必须是1~10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类名称必须是1~15位的字母数字组合',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.initCategory()
  },
  methods: {
    async initCategory() {
      const { data: res } = await getCategoryAPI()
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    addCategory() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message)
          // 重新获取图书
          this.initCategory()
          // 清空表单
          this.$refs.formRef.resetFields()
          // 关闭弹出层
          this.addVisible = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    resetAddForm() {
      // 清空表单
      this.$refs.formRef.resetFields()
      // 关闭弹出层
      this.addVisible = false
    },
    // 删除文章分类
    deleteRow(id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除id=1和id=2的数据')
      // 询问是否确认要删除
      this.$confirm('确定要删除吗？', '提示', {
        callback: async r => {
          // console.log(r) // cancel confirm
          if (r === 'confirm') {
            const { data: res } = await deleteCategoryAPI(id)
            if (res.code === 0) {
              this.$message.success(res.message)
              // 重新加载文章分类列表
              this.initCategory()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 显示修改的弹框
    editCategory(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('不允许修改前两条数据')
      this.editVisible = true
      // console.log(row)

      // 将当前行的信息赋值给数据项
      this.editData = { ...row }
    },
    // 确认修改文章分类
    updateCategory() {
      this.$refs.editRef.validate(async valid => {
        if (valid) {
          const { data: res } = await updateCategoryAPI(this.editData)
          if (res.code === 0) {
            this.$message.success(res.message)
            // 更新文章分类
            this.initCategory()
            // 关闭弹窗
            this.editVisible = false
          } else {
            this.$message.error(res.message)
          }
        }
      })
    },
    // 取消修改文章分类
    resetEditForm() {
      // 清空表单
      // this.$refs.editRef.resetFields()
      // 关闭弹出层
      this.editVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  font-size: 13px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
